package cn.stack.springframework.test;

import cn.bugstack.springframework.context.support.ClassPathXmlApplicationContext;
import cn.stack.springframework.test.bean.UserService3;
import org.junit.Test;
import org.openjdk.jol.info.ClassLayout;

public class ApiTest09 {
    @Test
    public void test_prototype(){
        //初始化 BeanFactory
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring3.xml");
        applicationContext.registerShutdownHook();

        //获取 Bean 对象调用方法
        UserService3 userService01 = applicationContext.getBean("userService", UserService3.class);
        UserService3 userService02 = applicationContext.getBean("userService", UserService3.class);

        //配置 scope="prototype/singleton"
        System.out.println(userService01);
        System.out.println(userService02);
        //打印十六进制哈希
        System.out.println(userService01+"十六进制哈希: "+Integer.toHexString(userService01.hashCode()));
        System.out.println(ClassLayout.parseInstance(userService01).toPrintable());
    }
    @Test
    public void test_factory_bean(){
        //初始化BeanFactory
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring3.xml");
        applicationContext.registerShutdownHook();

        //调用代理方法
        UserService3 userService = applicationContext.getBean("userService", UserService3.class);

        System.out.println("测试结果: "+userService.queryUserInfo());

    }
}
